<template>
    <div class="list-page">
        <div class="list-page-header list-page-header-flex">
            <div class="flex-left">
                <el-button type="primary" @click="add">新增</el-button>
                <el-button type="primary" @click="edit">编辑</el-button>
                <el-popconfirm title="删除人员后改人员无法登录系统，是否继续？" confirmButtonText='确定' cancelButtonText='取消' @confirm="del">
                    <template #reference>
                        <el-button>删除</el-button>
                    </template>
                </el-popconfirm>
            </div>
            <div class="flex-right">
                <el-input v-model="query.employeeNo" class="search-input-class" placeholder="工号" />
                <el-input v-model="query.name" class="search-input-class" placeholder="姓名" />
                <el-input v-model="query.departmentName" class="search-input-class" placeholder="所属部门" />
                <el-button type="primary" @click="loadData">查询</el-button>
                <el-button @click="reset">重置</el-button>
            </div>
        </div>
        <div class="list-content">
            <el-table ref="datagrid" :data="table.data" border @row-click="handleClickTableRow" @selection-change="handleSelectionChange">
                <el-table-column type="selection" width="55" />
                <el-table-column prop="employeeNo" label="工号" width="80" />
                <el-table-column prop="name" label="姓名" width="80" />
                <el-table-column prop="gender" label="性别" width="60" :formatter="genderFmt" />
                <el-table-column prop="educationName" label="学历" width="80" />
                <el-table-column prop="idCardNo" label="身份证号" show-overflow-tooltip />
                <el-table-column prop="departmentName" label="所属部门" show-overflow-tooltip />
                <el-table-column prop="kindName" label="类型" width="60" />
                <el-table-column prop="postName" label="岗位" width="120" show-overflow-tooltip />
                <el-table-column prop="professionalTitleName" label="职称" width="100" show-overflow-tooltip />
                <el-table-column prop="phone" label="联系电话" show-overflow-tooltip />
                <el-table-column prop="entryDate" label="入职时间" width="100" :formatter="formatUtil.gridDateFormat" />
                <el-table-column prop="state" label="状态" width="60" :formatter="stateFormatter" />
            </el-table>
        </div>
        <div class="list-footer">
            <el-pagination background @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="table.pagination.currentPage" :page-sizes="table.pagination.pageSizes" :page-size="table.pagination.pageSize" layout="total, sizes, prev, pager, next, jumper" :total="table.pagination.total">
            </el-pagination>
        </div>
        <transition name="slide-fade">
            <div v-if="showInfoWindow" class="list-form-modal">
                <employee-form :id="selectedEmployeeId" @close="closeInfoWindow"></employee-form>
            </div>
        </transition>
    </div>
</template>
<script>
import { ref, toRefs, reactive, onMounted } from 'vue'
import { ElNotification } from 'element-plus'
import { useRouter, useRoute } from 'vue-router'
import EmployeeForm from './form/EmployeeForm.vue'
import http from '@/http.js'
import formatUtil from '@/utils/format.js'

export default {
    name: 'Employee',
    components: {
        EmployeeForm
    },
    setup() {
        const router = useRouter();
        const route = useRoute();
        const datagrid = ref(null);

        const state = reactive({
            query: {
                employeeNo: '',
                name: '',
                departmentName: ''
            },
            table: {
                data: [],
                selected: [],
                pagination: {
                    currentPage: 1,
                    pageSizes: [10, 20, 50, 100],
                    pageSize: 10,
                    total: 0
                }
            },
            selectedEmployeeId: '',
            showInfoWindow: false
        });

        const handleSelectionChange = (val) => {
            state.table.selected = val;
        }

        const handleSizeChange = (val) => {
            state.table.pagination.pageSize = val;
            loadData();
        };

        const handleCurrentChange = (val) => {
            state.table.pagination.currentPage = val;
            loadData();
        };

        const handleClickTableRow = (row) => {
            datagrid.value.clearSelection();
            datagrid.value.toggleRowSelection(row);
        }

        const loadData = () => {
            let params = {
                employeeNo: state.query.employeeNo,
                name: state.query.name,
                departmentName: state.query.departmentName,
                page: state.table.pagination.currentPage,
                pageSize: state.table.pagination.pageSize
            }
            http.fetchForm({
                url: '/bas/employee/queryGrid',
                data: params
            }).then((res) => {
                state.table.data = res.data.grid;
                state.table.pagination.total = res.data.total;
            });
        }

        const genderFmt = (row, column, cellValue, index) => {
            let data = row[column.property];
            if (data == 0) {
                return "女";
            } else {
                return "男";
            }
        }

        const stateFormatter = (row, column, cellValue, index) => {
            let data = row[column.property];
            if (data == 0) {
                return "在职";
            } else {
                return "离职";
            }
        }

        const reset = () => {
            state.query.employeeNo = '';
            state.query.name = '';
            state.query.departmentName = '';
            loadData();
        }

        const add = () => {
            state.selectedEmployeeId = '';
            state.showInfoWindow = true;
        }

        const edit = () => {
            state.selectedEmployeeId = '';
            let selected = state.table.selected;
            if (selected.length == 0) {
                ElNotification({
                    title: '提示',
                    message: "请选择要编辑的人员",
                    type: 'info'
                });
                return;
            }
            state.selectedEmployeeId = selected[0].id;
            state.showInfoWindow = true;
        }

        const del = () => {
            let selected = state.table.selected;
            if (selected.length == 0) {
                ElNotification({
                    title: '提示',
                    message: "请选择要删除的人员",
                    type: 'info'
                });
                return;
            }
            http.fetchForm({
                url: '/bas/employee/deleteById',
                method: 'post',
                data: {
                    id: selected[0].id
                }
            }).then((res) => {
                ElNotification({
                    title: '提示',
                    message: "删除成功",
                    type: 'success'
                });
                loadData();
            });
        }

        const closeInfoWindow = () => {
            state.showInfoWindow = false;
            loadData();
        }

        // mounted
        onMounted(() => {
            loadData();
        });

        return {
            formatUtil,
            ...toRefs(state),
            datagrid,
            handleSelectionChange,
            handleSizeChange,
            handleCurrentChange,
            handleClickTableRow,
            loadData,
            stateFormatter,
            genderFmt,
            reset,
            add,
            edit,
            del,
            closeInfoWindow
        };
    }
}
</script>
<style scoped>
@import '@/assets/css/list-page.css';
</style>